home *** CD-ROM | disk | FTP | other *** search
- Path: holly.ACNS.ColoState.EDU!not-for-mail
- From: corbyh@holly.ACNS.ColoState.EDU (Corby S. Hudnall)
- Newsgroups: comp.lang.c++
- Subject: Re: passing arguments to main()
- Date: 4 Mar 1996 01:34:57 -0700
- Organization: Colorado State University, Fort Collins, CO 80523
- Message-ID: <4he9vh$4nu4@holly.ACNS.ColoState.EDU>
- References: <4h78vo$nqq@netnews.upenn.edu>
- NNTP-Posting-Host: holly.acns.colostate.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- Jamie Daniels (daniels@oasis.rad.upenn.edu) wrote:
- : Hello,
-
- : I have a question about c I am not very good with programming. I have this
- : project that I want to take two strings one a name the other being an 80
- : character message(sentence). I want it to spit out a simplt print
- statement at
- : an Unix prompt. But I don't know how to pass arguments to main().
- also I can't : get the output to be entered in at the unix prompt. I
- want the output to be a : command. for a simple example
-
- : printf("ping 165.234.323.222");
-
- : will not ping this address it only prints this statement before the prompt
-
- You want the "system" command not printf. Printf only outputs what you
- tell is to. It doesn't execute anything. try "man system" or "man 3
- system" so see which header file contains the prototype for system. I
- think that stdlib.h has it on most systems. As far as passing in command
- line arguments, use argc and argv.
-
- int main(int argc, char* argv[])
- {
- for(int i=0;i<argc;i++)
- cout << "Command line arguement" << (i+1) << " is: " << argv[i] << endl;
-
- cout << endl << "Now to ping that address" << endl ;
- system("ping 165.234.323.222"); // Pings the address you specified.
- // This is bad because is won't terminate.
-
- return 0;
- }
-
- This program prints out all the command line arguements. The best way to
- learn this is to practice.
- argc is an integer value of how many arguements were specified and argv
- is an array of pointers to chars. Check the online documentation. This
- is a pretty down and dirty explanation. You might also look at the
- spawn() and exec() family of functions to execute system commands.
-
- // ------------ BEGIN SIGNATURE ---------------
- #include <iostream.h>
- void main()
- {
- cout<<"\aName:\tCorby S. Hudnall\n";
- cout<<"School:\tColorado State University\n";
- cout<<"EMail\tcorbyh@holly.colostate.edu\n";
- cout<<"URL\thttp://holly.colostate.edu/~corbyh/\n";
- }
- // ------------- END SIGNATURE ----------------
-
-